#include <renderer.h>
#include <bigimage/perspectivecamera.h>
#include <bigimage/orbitcontrols.h>
#include <mdxl/mdxobject.h>
#include "previewcanvas.h"
#include "scene/previewscene.h"


CPreviewCanvas::CPreviewCanvas( int32 nLeft, int32 nTop, int32 nWidth, int32 nHeight ):
CImage( nLeft, nTop, nWidth, nHeight ){
    m_fCanvasWidth = nWidth;
    m_fCanvasHeight = nHeight;

}
CPreviewCanvas::~CPreviewCanvas(){

}
void CPreviewCanvas::Init(){
    m_rTextureTarget = NEW CTextureTarget( 256, 256, True, True );
    SetTextureCoordsInfo( 0, 256, 256, 0, m_fCanvasHeight, m_fCanvasWidth, 0 );
    ApplyTextureCoordsInfo();
    SetTexture( m_rTextureTarget.GetEntityData() );
    AddUIEvent( UEM_MOUSEEVENT );
    //AddUIEvent( UEM_CLICK );
    OnMouseEventObject = this;


    CPreviewScene* _pScene = NEW CPreviewScene();
    m_rPreviewScene = _pScene;
    m_rCamera = NEW CPerspectiveCamera( 45.0f, m_fCanvasWidth / m_fCanvasHeight, 0.1f, 1000.0f );
    m_rCamera->m_tPosition.Set( 500, 400, 0 );
    m_rCamera->m_tView.Set( 0, 0, 0 );
    m_rCamera->m_tUp.Set( 0.0f, 1.0f, 0.0f );
    m_rCamera->UpdateLook();

    m_rCameraControls = NEW COrbitControls( m_rCamera.GetEntityData(), m_fCanvasWidth, m_fCanvasHeight );
    m_rCameraControls->target.Set( 0, 0, 0 );
    m_rCameraControls->enableDamping = False;
    m_rCameraControls->dampingFactor = 0.25f;
    m_rCameraControls->screenSpacePanning = False;
    m_rCameraControls->minDistance = 150;
    m_rCameraControls->maxDistance = 1000;
    m_rCameraControls->maxPolarAngle = TWO_PI;
    _pScene->SetCamera( m_rCamera.GetEntityData() );
}
int32 CPreviewCanvas::Update( int32 nDelta, float fDelta ){
    m_rCameraControls->update();
    m_rPreviewScene->Update( nDelta, fDelta );
    return 0;
}
int32 CPreviewCanvas::Render(){
    m_rPreviewScene->Render( m_rTextureTarget.GetEntityData(), m_fCanvasWidth, m_fCanvasHeight );
    //m_rPreviewScene->Render( m_pTextureTarget.GetEntityData(), m_fCanvasHeight, m_fCanvasWidth );
    ////m_rTextureTarget->BeginRender( m_fCanvasWidth, m_fCanvasHeight );
    ////Renderer_SetClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
    ////Renderer_Clear( True, True );
    ////m_rTextureTarget->EndRender();

    return 0;
}
bool32 CPreviewCanvas::SetPreviewObjectAnimationName( const char* strAnimationName ){
    CMDXObject* _pMDXObject = (CMDXObject*)m_rCurrentPreviewGO.GetEntityData();
    return _pMDXObject->SetAnimation( strAnimationName );
}

void CPreviewCanvas::SetPreviewFile( const char* strFileName, const char* strTextureDir, const char* strPublicTextureDir ){
    if( m_rCurrentPreviewGO.IsAttached()){
        m_rPreviewScene->RemoveChild( m_rCurrentPreviewGO.GetEntityData() );
        m_rCurrentPreviewGO.Detach();        
    }
    m_rCamera->m_tPosition.Set( 500, 400, 0 );
    m_rCamera->m_tView.Set( 0, 0, 0 );
    m_rCamera->m_tUp.Set( 0.0f, 1.0f, 0.0f );
    m_rCamera->UpdateLook();
    m_rCameraControls->target.Set( 0, 0, 0 );

    CMDXParticleManager* _pParticleMgr = m_rPreviewScene->GetParticleManager();

    Vector3 _sPos = {0.0f, 0.0f, 0.0f};
    Vector3 _sRot = {0.0f, 0.0f, 0.0f};
    Vector3 _sScale = {1.0f, 1.0f, 1.0f};
    CMDXObject* _pMDXObject = NEW CMDXObject( _pParticleMgr, strPublicTextureDir );
    _pMDXObject->Init( strFileName,
                       _sPos, _sRot, _sScale,
                       strTextureDir,
                       strPublicTextureDir );

    m_rPreviewScene->AddChild( _pMDXObject );
    m_rCurrentPreviewGO = _pMDXObject;
    OnPreviewedFile( strFileName, _pMDXObject );
}
void CPreviewCanvas::OnMouseEvent( CUIElement* pSelf, EUIMouseMessage eMouseMessage, int32 x, int32 y, int32 z, int32 s ){
   // return;
    if( m_rPreviewScene.IsAttached() ){
        switch( eMouseMessage ){
        case UME_MOUSELDOWN:{
             m_rCameraControls->onMouseDown( ORBIT, x, y );
        }break;
        case UME_MOUSELUP:{
             m_rCameraControls->onMouseUp( ORBIT, x, y );
        }break;
        case UME_MOUSEMOVE:{
            m_rCameraControls->onMouseMove( x, y );
        }break;
        case UME_MOUSEWHEEL: m_rCameraControls->onMouseWheel( z ); break;
        }
    }
}


